package com.test.demos.service.impl;

import com.mybatisflex.core.query.QueryWrapper;
import com.test.demos.mapper.UsersMapper;
import com.test.demos.pojo.entity.UserInstitutionRelation;
import com.test.demos.mapper.UserInstitutionRelationMapper;
import com.test.demos.pojo.entity.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.test.demos.service.UserInstitutionRelationService;

import static com.test.demos.Constant.EMPLOYEE;
import static org.springframework.util.ObjectUtils.isEmpty;

/**
 * (UserInstitutionRelation)表服务接口
 *
 * @author twt
 * @since 2025-04-14 16:27:02
 */
@Service
@Transactional
public class UserInstitutionRelationServiceImpl implements UserInstitutionRelationService {

    @Autowired
    private UserInstitutionRelationMapper userInstitutionRelationMapper;
    /**
     * 通过ID查询单条数据
     *
     * @param relationId 主键
     * @return 实例对象
     */
    @Override
    public UserInstitutionRelation queryById(Integer relationId) {
        UserInstitutionRelation select = userInstitutionRelationMapper.selectOneById(relationId);
        if (isEmpty(select)) {
            throw new RuntimeException("未查询到对应数据");
        }
        return select;
    }

    /**
     * 插入数据
     *
     * @param userInstitutionRelation
     */
    @Override
    public void add(UserInstitutionRelation userInstitutionRelation) {
        Integer pkValue = userInstitutionRelation.getRelationId();

        if (userInstitutionRelationMapper.selectOneById(pkValue) != null) {
            throw new RuntimeException("数据已存在，无法插入");
        }
        int insert = userInstitutionRelationMapper.insert(userInstitutionRelation);
        if (insert != 1) {
            throw new RuntimeException("插入数据失败");
        }
    }



    /**
     * 更新数据
     *
     * @param userInstitutionRelation
     */
    @Override
    public void update(UserInstitutionRelation userInstitutionRelation) {
        int i = userInstitutionRelationMapper.update(userInstitutionRelation);
        if (i != 1) {
            throw new RuntimeException("更新数据失败");
        }
    }

    /**
     * 根据主键删除数据
     *
     * @param relationId
     */
    @Override
    public void deleteById(Integer relationId) {
        int i = userInstitutionRelationMapper.deleteById(relationId);
        if (i != 1) {
            throw new RuntimeException("删除数据失败");
        }
    }

}
